<?php

use Illuminate\Database\Seeder;

class AdministratorMenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
//        $paperExamQuestions = \App\Services\Designer\Model\PaperExamQuestion::query()->get();
//        foreach ($paperExamQuestions as $paperExamQuestion){
//            $paperExamQuestion->question_id = $paperExamQuestion->question_answer['id'];
//            $paperExamQuestion->save();
//        }
//
//        $questions = \App\Services\Designer\Model\Question::query()->get();
//        foreach ($questions as $question){
//            $question->exam_count = \App\Services\Designer\Model\PaperExamQuestion::query()->where('question_id',$question->id)->count();
//            $question->error_count = \App\Services\Designer\Model\PaperExamQuestion::query()->where('question_id',$question->id)->where('status',\App\Services\Designer\Model\PaperExamQuestion::ERROR)->count();
//            $question->save();
//        }

        $menus = [
            [
                'title' => '首页',
                'key' => 'Home',
                'icon' => 'icon-monitor',
            ],
            [
                'title' => '系统',
                'key' => 'system',
                'icon' => 'icon-cog',
                'children' => [
                    [
                        'title' => '配置',
                        'key' => 'Setting',
                        'permission' => 'setting',
                    ],
                ],
            ],
            [
                'title' => '管理员管理',
                'key' => 'administrator',
                'icon' => 'icon-head',
                'children' => [
                    [
                        'title' => '管理员',
                        'key' => 'Administrator',
                        'permission' => 'administrator',
                    ],
                    [
                        'title' => '管理员角色',
                        'key' => 'AdministratorRole',
                        'permission' => 'administrator_role',
                    ],
                ],
            ],
            [
                'title' => 'Banner管理',
                'key' => 'banner',
                'icon' => 'icon-map',
                'children' => [
                    [
                        'title' => 'Banner图',
                        'key' => 'Banner',
                        'permission' => 'banner_serve',
                    ],
                ],
            ],
            [
                'title' => '经典案例管理',
                'key' => 'classic_case',
                'icon' => 'icon-map',
                'children' => [
                    [
                        'title' => '经典案例管理',
                        'key' => 'ClassicCase',
                        'permission' => 'classic_case_serve',
                    ],
                ],
            ],
            [
                'title' => '720全景图库管理',
                'key' => 'space_scheme',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => '720全景图库管理',
                        'key' => 'SpaceScheme',
                        'permission' => 'spacescheme_serve',
                    ],
                ],
            ],
            [
                'title' => '设计师管理',
                'key' => 'designer',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => '设计师管理',
                        'key' => 'Designer',
                        'permission' => 'designer_serve',
                    ],
                ],
            ],
        ];
        // DB::statement('truncate table administrator_menus');
        foreach ($menus as $index => $menu) {
            $sort = ($index + 1) * 10;
            $model = $this->create($menu, 0, $sort);
            if ($model === false) {
                continue;
            }
            $children = $menu['children'] ?? [];
            if (!$children) {
                continue;
            }
            foreach ($children as $i => $item) {
                $this->create($item, $model->id, $sort * 10 + ($i + 1) * 10);
            }
        }
    }

    protected function create($menu, $parentId = 0, $sort = 0)
    {
        $exists = \App\Models\AdministratorMenu::query()->where('name', $menu['title'])->where('url', $menu['key'])->exists();
        if ($exists) {
            return \App\Models\AdministratorMenu::query()->where('name', $menu['title'])->where('url', $menu['key'])->firstOrFail();
        }
        return \App\Models\AdministratorMenu::create([
            'parent_id' => $parentId,
            'name' => $menu['title'],
            'url' => $menu['key'] ?? '',
            'permission' => $menu['permission'] ?? '',
            'icon' => $menu['icon'] ?? '',
            'is_super' => $menu['super'] ?? 0,
            'sort' => $sort,
        ]);
    }
}
